/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.dao.impl;

import es.silenus.detecta.dao.SurveyUnitAnswerImportedDAO;

import es.silenus.detecta.model.SurveyAnswerHeaderImported;
import es.silenus.detecta.model.SurveyAnswerValidation;
import es.silenus.detecta.model.SurveyUnitAnswerImported;
import es.silenus.detecta.model.SurveyUnitAnswerImportedId;

import org.hibernate.FetchMode;
import org.hibernate.Hibernate;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


/**
 * Survey unit answer imported DAO implementation.
 *
 * @author <a href="mailto:dgarcia@silenus-consultoria.es">Daniel Garc&iacute;a</a>
 *
 * @since 02-abr-2009 13:07:58
 */
public class SurveyUnitAnswerImportedDAOImpl extends ReadWriteDAOImpl<SurveyUnitAnswerImported, SurveyUnitAnswerImportedId>
	implements SurveyUnitAnswerImportedDAO {
	/**
	 * Finds a survey unit answer by its id.
	 *
	 * @param header the header.
	 * @param questionOrder the question order.
	 *
	 * @return the survey unit answer.
	 */
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public SurveyUnitAnswerImported findById(final SurveyAnswerHeaderImported header, int questionOrder) {
		SurveyUnitAnswerImportedId id = new SurveyUnitAnswerImportedId();

		id.setHeader(header);
		id.setQuestionNumber(questionOrder);

		return findById(id);
	}

	/**
	 * Finds a survey unit answer by its id.
	 *
	 * @param header the header.
	 * @param questionOrder the question order.
	 *
	 * @return the survey unit answer.
	 */
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public SurveyUnitAnswerImported findById(long header, int questionOrder) {
		SurveyUnitAnswerImportedId id = new SurveyUnitAnswerImportedId();
		SurveyAnswerHeaderImported headerObject = new SurveyAnswerHeaderImported();

		headerObject.setId(header);
		id.setHeader(headerObject);
		id.setQuestionNumber(questionOrder);

		return findById(id);
	}

	/**
	 * Override this method to provide a find by example page criteria.
	 *
	 * @param criteria the criteria.
	 * @param object the sample object.
	 *
	 * @return the criteria.
	 */
	protected DetachedCriteria preparePageCriteria(final DetachedCriteria criteria, final Map<String, Object> object) {
		criteria.setFetchMode("header", FetchMode.JOIN);

		Object temp = object.get("idHeader");

		if(temp != null) {
			criteria.add(Restrictions.sqlRestriction("{alias}.id_header=?", temp, Hibernate.LONG));
		}

		temp = object.get("questionId");

		if(temp != null) {
			criteria.add(Restrictions.eq("question", temp));
		}

		temp = object.get("questionCode");

		if(temp != null) {
			criteria.add(Restrictions.ilike("questionCode", temp));
		}

		temp = object.get("statusId");

		if(temp != null) {
			criteria.add(Restrictions.eq("status", SurveyAnswerValidation.getById((Integer)temp)));
		}

		temp = object.get("preferenceId");

		if(temp != null) {
			criteria.add(Restrictions.eq("preference.id", temp));
		}

		return criteria;
	}

	/**
	 * Override this method to provide a find by example list criteria.
	 *
	 * @param criteria the criteria.
	 * @param object the sample object.
	 *
	 * @return the criteria.
	 */
	protected DetachedCriteria prepareListCriteria(final DetachedCriteria criteria, final Map<String, Object> object) {
		return preparePageCriteria(criteria, object);
	}
}
